using UnityEngine;
using System.Collections;

public class MoveShape2 : MonoBehaviour {
	
//	public Shader shader = Shader.Find("Transparent/Diffuse");
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	}
	
	Vector3 screenPointFromShape;
	Vector3 offset;
	float initMouseX;
	bool onDrag;
	
	void OnMouseDown()
	{
		
		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		RaycastHit hit;
		
		int layerMask = 1 << 8;
		
		if (Physics.Raycast(ray, out hit, 100, layerMask)) {
		
			Debug.DrawLine(ray.origin, hit.point);
			offset = hit.point - transform.position;
			//offset.y = transform.position.y;
		}
		initMouseX = Input.mousePosition.x;
		onDrag = true;
		
		
		Color objectColor = renderer.material.color;
		objectColor.a = 0.5f;
		renderer.material.color = objectColor;
	}
	
	void OnMouseUp () {
		onDrag = false;
		Color objectColor = renderer.material.color;
		objectColor.a = 1;
		renderer.material.color = objectColor;
	}
	
	void OnMouseDrag () {
		
		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		RaycastHit hit;
		
		int layerMask = 1 << 8;
		
		if (Physics.Raycast(ray, out hit, 100, layerMask)) {
			Debug.DrawLine(ray.origin, hit.point);
			transform.position = hit.point - offset ;//+ new Vector3(0, transform.localScale / 2, 0);//offset;
		}
	}

	void OnTriggerEnter(Collider other) {
    }
}
